<?php

namespace admin\controller;
use Core\common\Controller;
use Core\common\Auth;
use model\menu;
use QRcode;
class _base extends Controller
{

    public function __construct()
    {
        global $_W;
        session_start();

        $this->checkLogin();
        if(!$this->checkAuth()){
            create_info('非法操作','', 'error');
        }
        $this->menus = (new menu)->getTreeData("level");

    }

    public function checkLogin(){
        global $_W;
        $do = I('do');
        $rule = [
            'admin/login','admin/loginOut','index/auth'
        ];
        foreach ($rule as $k=>$v){
            if($do == $v){
                return true;
            }
        }
        if (empty($_W['uid'])) {
            header('Location:' . gWebUrl('admin/login'));
        }
        return true;
    }


    public function checkAuth(){
        global $_W;
        $do = I('do');
        $rule = [
            'admin/login','admin/loginOut','index/auth','index/index'
        ];
        foreach ($rule as $k=>$v){
            if($do == $v){
                return true;
            }
        }
        if (empty($_W['uid'])) {
            header('Location:' . gWebUrl('admin/login'));
        }
        //判断用户是否权限验证
        $aParams['field'] = ['create_uid'];
        $aParams['where'] = ['uniacid'=>uniacid()];
        $create_uid = M('uni_account',false)->getData($aParams);
        if($create_uid['create_uid'] != $_W['uid']){
            $result = (new Auth)->check($do,$_W['uid']);
            if (!$result) {

                create_info('您没有权限',gWebUrl('index/auth'), 'error');
                exit();
            }
        }
        return true;
    }

    /**
     * 添加日志
     *
     * @param array $array [添加条件]
     *
     * @author luzehua
     * @date   2018-05-13
     *
     */
    function log_act($content)
    {
        global $_W;
        $do = I('do') == 'index'?'index/index':I('do');
        $controller = explode("/",$do);

        $admin = M('users',false)->getData(['field'=>['username'],'where'=>['uid' => $_W['uid']]]);

        $info = array(
            'uniacid'=>uniacid(),
            'class_name' => $controller[0],
            'func_name' => $controller[1],
            'content' => $content,
            'admin_name'=>$admin['username'],
            'uid' => $_W['uid'],
            'act_ip' => $this->func_get_ip(),
            'creation_date' => time(),
        );
        if ($info)
            $aResult = M('log_act')->add($info);

        if ($aResult) return true;
        else return FALSE;
    }


    /**
     * @Notes:密码加密
     * @Interface func_passwd
     * @param $passwd
     * @param string $salt
     * @return string
     * @author: LZH
     * @Time: 2019/5/20   16:36
     */
    function func_passwd($passwd, $salt = '')
    {
        return md5(md5($passwd) . $salt);
    }

    /**
     * @Notes:随机字符串
     * @Interface func_salt
     * @param int $num
     * @return string
     * @author: LZH
     * @Time: 2019/5/20   16:37
     */
    function func_salt($num = 6)
    {
        $sStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $sSalt = "";

        for ($i = 0; $i < $num; $i++) {
            $sSalt .= $sStr{mt_rand(0, 32)};    //生成php随机数
        }

        return $sSalt;
    }

    /**
     * @Notes:获取ip地址
     * @Interface func_get_ip
     * @param int $type
     * @return mixed
     * @author: LZH
     * @Time: 2019/5/20   16:47
     */
    function func_get_ip($type = 0)
    {
        $type = $type ? 1 : 0;
        static $ip = null;
        if ($ip !== null)
            return $ip[$type];
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos = array_search('unknown', $arr);
            if (false !== $pos)
                unset($arr[$pos]);
            $ip = trim($arr[0]);
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = ip2long($ip);
        $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
    }

    /**
     * 生成礼包接口 100W数据同时生成不重复
     * @return string
     */
    function sp_gm_get_gift_code($number,$id,$num) {
        for ($i=0;$i<$number;$i++){
            $uniqid = uniqid($id,true);
            $param_string = time().rand().$uniqid;
            $sha1 = sha1($param_string);
            for(
                $a=$sha1,
                $s = '0123456789abcdefghijklmnopqrstuvwxyz',
                $d = '',
                $f = 0;
                $f < $num;
                $g = ord( $a[ $f ] ),
                $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
                $f++
            );
            $data[] = $d;
        }
        return $data;
    }


    /**
     *  生成二维码
     */
    public function VerificationCode($data){
        global $_W;
        include_once MODULE_ROOT. '/Core/phpqrcode/phpqrcode.php';
        // 二维码数据
        $data = $_W['siteroot'].$data;
        //定义文件名
        $filename = md5($data).'.png';

        // 生成的文件名
        $path = MODULE_ROOT.'/static/uploads';
        $aUpload  = "/clerk/";//上传目录

        if (!file_exists($path.$aUpload)){
            mkdir ($path.$aUpload,0777,true);
        }
        $filenames = $path.$aUpload.$filename;

        if(!file_exists($filenames))
        {
            // 纠错级别：L、M、Q、H
            $errorCorrectionLevel = 'L';
            // 点的大小：1到10
            $matrixPointSize = 4;
            QRcode::png($data, $filenames, $errorCorrectionLevel, $matrixPointSize, 2);
        }
        $url = $_W['siteroot'].'addons/'.MOD_ADD.'/static/uploads/clerk/'.$filename;
        return $url;
    }

    /**
     * 退款订单
     * @param $order_type
     * @return string
     */
    public function getOrderRefundNo() {
        return 'Re' . mt_rand(10, 99) . time() . mt_rand(10, 99);
    }

}

